package com.thinkgem.jeesite.modules.tb_waybill.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.ToolsUtil;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.web.ErrorCodeAndMsg;
import com.thinkgem.jeesite.modules.tb_waybill.dao.TbWaybillDetailDao;
import com.thinkgem.jeesite.modules.tb_waybill.entity.TbWaybill;
import com.thinkgem.jeesite.modules.tb_waybill.entity.TbWaybillDetail;
import com.thinkgem.jeesite.modules.tb_waybill.service.TbWaybillService;
/**
 * 司机任务列表
 * @author zhangYangZe
 *
 */
@Controller
@RequestMapping("/app/tbWaybill/")
public class TbWaybillAppController extends BaseController{

	@Autowired
	private TbWaybillService tbWaybillService;
	@Autowired
	private TbWaybillDetailDao tbWaybillDetailDao;
	/**
	 * 任务列表
	 * @param request
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> list(HttpServletRequest request,HttpServletResponse response){
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		int driverId = uid(request);
		if(driverId==-1)return returnError(ErrorCodeAndMsg.USER_NULL_CODE, ErrorCodeAndMsg.USER_NULL_CODE_MSG);
		try{
			TbWaybill tbWaybill = new TbWaybill();
			tbWaybill.setDriverId(driverId+"");
			tbWaybill.setState("0");
			Page<TbWaybill> page = tbWaybillService.findPage(new Page<TbWaybill>(request, response), tbWaybill); 
			List<TbWaybill> list = page.getList();			
			if(list.size()>0){
				for(int i =0;i<list.size();i++){
					TbWaybill waybill = list.get(i);
					TbWaybillDetail tbWaybillDetail = new TbWaybillDetail();
					tbWaybillDetail.setWaybillId(waybill.getId());
					List<TbWaybillDetail> l = tbWaybillDetailDao.findAllList(tbWaybillDetail);
					waybill.setNumber(l.size());
					int saze = l.size();
					switch (saze) {
						case 1:
							waybill.setTbWaybillDetailOne(l.get(0));
							break;
						case 2:
							waybill.setTbWaybillDetailOne(l.get(0));
							waybill.setTbWaybillDetailTwo(l.get(1));
							break;
						case 3:
							waybill.setTbWaybillDetailOne(l.get(0));
							waybill.setTbWaybillDetailTwo(l.get(1));
							waybill.setTbWaybillDetailThree(l.get(2));
							break;
					default:
						break;
					}
					Map<String,Object> m = new HashMap<String, Object>();
					if(waybill.getId() != null){
						m.put("wabbill_id", waybill.getId());//运单ID
					}
					//车俩信息
					if(waybill.getTbCarInfo() != null){
						m.put("carInfo_id", waybill.getTbCarInfo().getId());//车辆ID
						m.put("carInfo_carCode",waybill.getTbCarInfo().getCarCode());//车牌号
						m.put("carInfo_carGCode",waybill.getTbCarInfo().getCarGCode());//挂车号
					}
					//司机信息
					if(waybill.getTbDriver() != null){
						m.put("driver_id", waybill.getTbDriver().getId());//司机ID
						m.put("driver_name", waybill.getTbDriver().getDriverName());//司机名称
					}
					if(waybill.getTbDriverTwo() != null){
						m.put("driverTwo_id", waybill.getTbDriverTwo().getId());//司机ID （2）
						m.put("driverTwo_name", waybill.getTbDriverTwo().getDriverName() );//司机名称
					}
					if(waybill.getTbLoadingAddress() != null){
						m.put("loadingAddress_address", waybill.getTbLoadingAddress().getShortName());//装货地址
					}
					if(waybill.getTbLoadingAddressTwo() != null){
						m.put("loadingAddressTwo_address", waybill.getTbLoadingAddressTwo().getShortName());//装货地址(2)
					}
					if(waybill.getState() != null){
						m.put("wabbill_state", waybill.getState());//0等待指派1出车登记中2等待运输3运输中4等待报账5审核中6完结9司机接受任务
					}
					if(waybill.getNumber()+"" != null){
						m.put("wabbill_number", waybill.getNumber()+"");//拉货仓数
					}
					Date d = waybill.getOutCarTime();
					m.put("wabbill_outCarTime", ToolsUtil.dateFormate(d, "yyyy-MM-dd HH:mm"));	
					m.put("tbWaybillDetailOne", gainValue(waybill.getTbWaybillDetailOne()) );
					m.put("tbWaybillDetailTwo", gainValue(waybill.getTbWaybillDetailTwo()) );
					m.put("tbWaybillDetailThree", gainValue(waybill.getTbWaybillDetailThree()) );					
					result.add(m);
				}
			}			
		}catch(Exception e){
			return returnError(">>>司机任务列表异常", e);
		}
		return returnSuccess(result);
	}
		
	public Map<String,String> gainValue(TbWaybillDetail tbWaybillDetail){
		Map<String,String> m = new HashMap<String, String>();		
		if(tbWaybillDetail != null){
			m.put("tbWaybillDetail_good_id",tbWaybillDetail.getTbGoods().getId() );
			m.put("tbWaybillDetail_good", tbWaybillDetail.getTbGoods().getGoodName() );
			m.put("tbWaybillDetail_weight", tbWaybillDetail.getWeight()+"" );
			m.put("tbWaybillDetail_dischargeCargoAddress", tbWaybillDetail.getTbDischargeCargoAddress().getShortName());
		}else{
			m.put("tbWaybillDetail_good_id","" );
			m.put("tbWaybillDetail_good",  "");
			m.put("tbWaybillDetail_weight", "" );
			m.put("tbWaybillDetail_dischargeCargoAddress",  "");
		}
		return m;
	}
	
	
}
